<?php
/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2022/2/21
 * Time: 14:58
 */

namespace App\Console\Commands;
use App\Http\Model\Hint;

use App\Http\Services\Queue\QueueServices;
use Illuminate\Console\Command;

class Queue extends Command{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Queue {queueName}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '执行队列(MySql) Queue {queueName}';

    protected $service = null;

    protected $limit_time = 280;// 最大执行时长

    protected $queueStartTime = 0;// 本次运行开始时间
    protected $queueEndTime = 0;// 本次运行开始时间


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this -> queueStartTime = time();
        $this -> queueEndTime = $this -> queueStartTime + $this -> limit_time;
        $this -> service = new QueueServices();

    }

    public function handle(){
        $queueName = $this->argument('queueName');
        while (true){
            if(time() > $this -> queueEndTime){
                break;
            }

            $queueInfo = $this -> service -> pop($queueName);
            if (empty($queueInfo)){
                sleep(10);
                continue;
            }
            $this -> service -> queueLock($queueInfo['id']);
            $this -> service -> handle($queueInfo);
            $this -> service -> queueEnd($queueInfo['id']);
        }
        return true;
    }







}
